package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbStudentCollect;
import com.baike.service.impl.TbStudentCollectServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbs
 * @since 2021-10-30
 */
@RestController
@RequestMapping("studentCollectController")
@Api("学生收藏课程数据管理中心")
public class TbStudentCollectController {

    @Autowired
    private TbStudentCollectServiceImpl collectService;

    @PostMapping
    @ApiOperation("增加学生收藏课程数据")
    @ApiImplicitParam(value = "收藏管理实体数据",name = "studentCollect",required = true,paramType = "body")
    public Result postStudentCollent(@RequestBody TbStudentCollect studentCollect){
        // 空参判断
        if(Objects.nonNull(studentCollect.getStudentId()) && Objects.nonNull(studentCollect.getCourseId())){
            //数据添加状态判断
            boolean b = collectService.save(studentCollect);
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }
        }

        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @DeleteMapping
    @ApiOperation("根据学生id 课程id数组 删除收藏数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学生id" , name = "sId" ,required = true ,paramType = "query"),
            @ApiImplicitParam(value = "课程id数组" , name = "cIds" ,required = true ,paramType = "query")
    })
    public Result delStudentCollent(@RequestParam("sId")Long sId,@RequestParam("cIds")Long[] cIds){
        //非空判断
        if(Objects.nonNull(sId) && Objects.nonNull(cIds)){
            LambdaQueryWrapper<TbStudentCollect> wrapper = Wrappers.<TbStudentCollect>lambdaQuery()
                    .eq(TbStudentCollect::getStudentId, sId)
                    .in(TbStudentCollect::getCourseId, Arrays.asList(cIds));
            boolean b = collectService.remove(wrapper);
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @PostMapping("findCollectBySid")
    @ApiOperation("根据学生id 查询收藏记录")
    @ApiImplicitParam(value = "学生id",name = "sId",required = true,paramType = "query")
    public Result findCollectBySid(@RequestParam("sId")Long sId){
        // 非空判断
        if(Objects.nonNull(sId)){
            LambdaQueryWrapper<TbStudentCollect> wrapper = Wrappers.<TbStudentCollect>lambdaQuery().eq(TbStudentCollect::getStudentId, sId);
            List<TbStudentCollect> list = collectService.list(wrapper);
            if(list.size() > 0){
                return Result.build(ConstantEnum.SUCCESS).setResult(list);
            }else {
                return Result.build(ConstantEnum.NULL_DATA);
            }
        }
        return Result.build(ConstantEnum.ERROR);
    }



}
